package com.xmotor.util;


import org.apache.commons.lang3.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtils {

    /**
     * 获取YYYYMMDD格式
     *
     * @return
     */
    public static String getDays() {
        SimpleDateFormat sdfDays = new SimpleDateFormat("yyyyMMdd");
        return sdfDays.format(new Date());
    }

    /**
     * yyyy年MM月dd日
     *
     * @return
     */
    public static String getDays2() {
        SimpleDateFormat sdfTimes = new SimpleDateFormat("yyyy年MM月dd日");
        return sdfTimes.format(new Date());
    }

    /**
     * yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String getDays3() {
        DateFormat sdfTimes = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdfTimes.format(new Date());
    }

    /**
     * 日期格式 yyyy-MM-dd
     *
     * @return
     */
    public static String getDays4() {
        DateFormat sdfTimes = new SimpleDateFormat("yyyy-MM-dd");
        return sdfTimes.format(new Date());
    }

    /**
     * yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String getDays5(Date date) {
        DateFormat sdfTimes = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdfTimes.format(date);
    }

    /**
     * 获取YYYY格式
     *
     * @return
     */
    public static String getYear(Date date) {
        SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
        return sdfYear.format(date);
    }

    /**
     * 获取YYYY格式
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static String getYear(String date) throws ParseException {
        SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return getYear(sdfTime.parse(date));
    }

    /**
     * @param s
     * @param e
     * @return boolean
     * @throws
     * @Title: compareDate
     * @Description: TODO(日期比较 ， 如果s > = e 返回true 否则返回false)
     * @author luguosui
     */
    public static boolean compareDate(String s, String e, String format) {

        Date sd = fomatDate(s, format);
        Date ed = fomatDate(e, format);

        if (sd == null || ed == null) {
            return false;
        }
        return sd.getTime() >= ed.getTime();
    }

    /**
     * 格式化日期
     *
     * @return
     */
    public static Date fomatDate(String date, String format) {
        DateFormat fmt = new SimpleDateFormat(format);
        try {
            return fmt.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取当前日期是星期几
     *
     * @param dt
     * @return 当前日期是星期几
     */
    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) w = 0;
        return weekDays[w];
    }

    /**
     * <li>功能描述：时间相减得到天数
     *
     * @param beginDateStr
     * @param endDateStr
     * @return long
     * @author Administrator
     */
    public static long getDaySub(String beginDateStr, String endDateStr) {
        long day = 0;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate = null;
        Date endDate = null;

        try {
            beginDate = format.parse(beginDateStr);
            endDate = format.parse(endDateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (beginDate != null && endDate != null) {
            day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
        }
        //System.out.println("相隔的天数="+day);

        return day;
    }

    /**
     * 两个时间相差距离多少天多少小时多少分多少秒
     *
     * @param beginDate 时间参数 1 格式：1990-01-01 12:00:00
     * @param endDate   时间参数 2 格式：2009-01-01 12:00:00
     * @return long[] 返回值为：{天, 时, 分, 秒}
     */
    public static long[] getDistanceTimes(Date beginDate, Date endDate) {
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        try {
            long time1 = beginDate.getTime();
            long time2 = endDate.getTime();
            long diff;
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            day = diff / (24 * 60 * 60 * 1000);
            hour = (diff / (60 * 60 * 1000) - day * 24);
            min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
            sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        } catch (Exception e) {
            e.printStackTrace();
        }

        long[] times = {day, hour, min, sec};
        return times;
    }

    /**
     * 两个时间相差多少分钟
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public static long getDistanceMins(Date beginDate, Date endDate) {

        long min = 0;

        long time1 = beginDate.getTime();
        long time2 = endDate.getTime();
        long diff;
        if (time1 < time2) {
            diff = time2 - time1;
        } else {
            diff = time1 - time2;
        }

        return min = diff / (60 * 1000);
    }

    /**
     * 得到n天之后的日期
     *
     * @param days
     * @return
     */
    public static String getAfterDayDate(String days) {

        return getAfterDayDate(null, days, "yyyy-MM-dd");
    }

    /**
     * 得到n天之后的日期
     *
     * @param days
     * @return
     */
    public static String getAfterDayDate(String startDate, String days) {

        return getAfterDayDate(startDate, days, "yyyy-MM-dd");
    }

    /**
     * 得到n天之后的日期
     *
     * @param startDate 起始日期
     * @param days
     * @param format
     * @return
     */
    public static String getAfterDayDate(String startDate, String days, String format) {

        int daysInt = Integer.parseInt(days);

        Calendar canlendar = Calendar.getInstance(); // java.util包
        if (StringUtils.isNotBlank(startDate)) {
            canlendar.setTime(DateUtils.fomatDate(startDate, format));
        }
        canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
        Date date = canlendar.getTime();

        SimpleDateFormat sdfd = new SimpleDateFormat(format);
        String dateStr = sdfd.format(date);

        return dateStr;
    }

    /**
     * 获取n月之后日期
     *
     * @param month
     * @return
     */
    public static String getAfterMonth(String startDate, String month) {
        return getAfterMonth(startDate, month, "yyyy-MM-dd");
    }

    /**
     * 获取n年之后日期
     *
     * @param year
     * @return
     */
    public static String getAfterYears(String startDate, String year) {
        return getAfterYears(startDate, year, "yyyy-MM-dd");
    }

    /**
     * 获取n年之后日期
     *
     * @param year
     * @param format
     * @return
     */
    public static String getAfterYears(String startDate, String year, String format) {

        int yearInt = Integer.parseInt(year);

        Date date = new Date();//取时间

        Calendar calendar = Calendar.getInstance();
        if (StringUtils.isNotBlank(startDate)) {
            calendar.setTime(DateUtils.fomatDate(startDate, format));
        } else {
            calendar.setTime(date);

        }
        calendar.add(calendar.YEAR, yearInt);
        date = calendar.getTime();
        DateFormat df = new SimpleDateFormat(format);
        return df.format(date);
    }

    /**
     * 获取n月之后日期
     *
     * @param month
     * @param format
     * @return
     */
    public static String getAfterMonth(String startDate, String month, String format) {

        int monthInt = Integer.parseInt(month);

        Calendar c = Calendar.getInstance();//获得一个日历的实例
        if (StringUtils.isNotBlank(startDate)) {
            c.setTime(DateUtils.fomatDate(startDate, format));
        } else {
            Date date = new Date();
            c.setTime(date);                            //设置日历时间
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        c.add(Calendar.MONTH, monthInt);               //在日历的月份上增加6个月
        String strDate = sdf.format(c.getTime());   //的到你想要得6个月后的日期
        return strDate;
    }

    /**
     * 格式化日期
     *
     * @param date
     * @return
     */
    public static String formatDate(Date date, String format) {
        SimpleDateFormat sdfDays = new SimpleDateFormat(format);
        return sdfDays.format(date);
    }

    /**
     * 给时间加上几个小时
     *
     * @param date 当前时间 格式：yyyy-MM-dd HH:mm:ss
     * @param hour 需要加的时间
     * @return
     */
    public static Date addDateMinut(Date date, int hour) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR, hour);// 24小时制
        return cal.getTime();
    }

    /**
     * 获取每月第一天开始的时间
     *
     * @return
     */
    public static String getFirstDay() {
        return String.format("%04d-%02d-%02d 00:00:00", Calendar.getInstance().get(Calendar.YEAR),
                Calendar.getInstance().get(Calendar.MONTH) + 1,
                Calendar.getInstance().getActualMinimum(Calendar.DAY_OF_MONTH));
    }

    /**
     * 获取当前日期对应月份的最后一天结束的时间
     *
     * @return
     */
    public static String getLastDay() {
        return String.format("%04d-%02d-%02d 23:59:59", Calendar.getInstance().get(Calendar.YEAR),
                Calendar.getInstance().get(Calendar.MONTH) + 1,
                Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH));
    }

    /**
     * 获取当前日期开始的时间
     *
     * @return
     */
    public static String getDayStart() {
        return String.format("%04d-%02d-%02d 00:00:00", Calendar.getInstance().get(Calendar.YEAR),
                Calendar.getInstance().get(Calendar.MONTH) + 1,
                Calendar.getInstance().get(Calendar.DATE));
    }

    /**
     * 获取当前日期结束的时间
     *
     * @return
     */
    public static String getDayEnd() {
        return String.format("%04d-%02d-%02d 23:59:59", Calendar.getInstance().get(Calendar.YEAR),
                Calendar.getInstance().get(Calendar.MONTH) + 1,
                Calendar.getInstance().get(Calendar.DATE));
    }

    /**
     * 获取日期的区间
     * @param startDay
     * @param endDay
     * @return
     * @throws Exception
     */
//    public static ArrayList<String> getDayIntervals(String startDay, String endDay) throws Exception {
//        ArrayList<String> arrayList = new ArrayList<>();
//        Calendar calendar = Calendar.getInstance();
//        Date date = new SimpleDateFormat("yyyy-MM-dd").parse(startDay);
//        calendar.setTime(date);
//        String dateNow = startDay;
//        arrayList.add(startDay);
//        while(!dateNow.equals(endDay)){
//            calendar.add(Calendar.DATE, 1);
//            dateNow = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
//            arrayList.add(dateNow);
//        }
//        return arrayList;
//    }

    /**
     * 获取日期的区间
     *
     * @param startDay
     * @param endDay
     * @return
     * @throws Exception
     */
    public static ArrayList<String> getDayIntervals(String startDay, String endDay) throws Exception {
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add(startDay);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate = sdf.parse(startDay);
        Date endDate = sdf.parse(endDay);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(beginDate);
        while (endDate.after(calendar.getTime())) {
            calendar.add(Calendar.DATE, 1);
            arrayList.add(sdf.format(calendar.getTime()));
        }
        return arrayList;
    }


    public static void main(String[] args) throws Exception {
        System.out.println(DateUtils.getAfterDayDate("2018-03-01", "1"));
        System.out.println(DateUtils.getAfterMonth("2018-03-01", "1"));
        System.out.println(DateUtils.getAfterYears("2018-03-01", "1"));
    }
}

